package com.tbit.uqbike.quartz;

import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.AgentDepositVipDao;
import com.tbit.uqbike.webmanager.dao.core.CouponUserDao;
import com.tbit.uqbike.webmanager.util.LogUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 16:48 2020/4/7
 */
@Slf4j
@Component
public class ClearExpireCouponQuartz {

    @Autowired
    private CouponUserDao couponUserDao;
    @Autowired
    private AgentDepositVipDao agentDepositVipDao;
    @Autowired
    private AccountDao accountDao;


    @Scheduled(cron = "0 0 1 * * ?")//0
//  @Scheduled(cron="0 57 13 * * ?")
    public void clear() {
        LogUtil.info("开始自动清除失效优惠券");
        couponUserDao.editExpire();
   /* List<CouponUser> couponUsers= couponUserDao.getExpire();
    List<Integer> ids = new ArrayList<>();
    List<Integer> expires;
    if(couponUsers.size()>0){
      for (CouponUser couponUser:couponUsers) {
        ids.add(couponUser.getCouponuserId());
      }
    }
    while(ids.size()>0){
      if(ids.size()>100000){
        expires = new ArrayList<>(ids.subList(0,100000));
        ids=ids.subList(100000,ids.size());
      }else{
        expires=new ArrayList<>(ids);
        ids=new ArrayList<>();
      }
      couponUserDao.editExpire(expires);
    }*/
    }

    @Scheduled(cron = "0 30 1 * * ?")//0
    public void clearOverdueWaived() {
        Map<String, Object> params = new HashMap<>();
        params.put("type", "3");
        List<Integer> detects = new LinkedList<>();
        List<Account> accounts = accountDao.getAccountByType(params);
        if (null != accounts && accounts.size() > 0) {
            detects = accounts.stream().map(Account::getAccountId).collect(Collectors.toList());
        }
        detects.forEach(accountId -> {
            log.info("开始自动清除区域{}过期免押信息", accountId);
            agentDepositVipDao.delExpired(accountId);
        });


    }


}
